package cn.hlife.com.hlfhaemocyte.bluetooth;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.widget.ProgressBar;

import com.blelibrary.BleOperate;
import com.blelibrary.SearchBleMacResultListener;
import com.utils.LogUtils;

import cn.hlife.com.hlfhaemocyte.utils.DialogUtils;

/**
 * Created by Administrator on 2017/5/26.
 */

public class BluetoothPercentor {
    Dialog loadindDialog;

    public interface BuletoothPercentorListener {
        /**
         * 没有mac地址
         */
        void noMacAddress();
        /**
         * 设备已经连接
         */
        void hasConnected();
        /**
         * 蓝牙连接超时
         */
        void connectTimeout();

        /**
         * 连接成功
         */
        void findUUIDNotify();

        /**
         * 没有发现蓝牙设备
         */
        void noFindBuletoothDevice();


        /**
         * 蓝牙数据的返回
         */
        void onResultReceive(byte[] result);
        /**
         * 蓝牙断开
         */
        void disconnect(String macAddress);
    }

    /**
     * /**
     * 初始化蓝牙并且发送通讯指令
     *
     * @param context
     * @param blueToothMac               蓝牙地址
     * @param buletoothPercentorListener 监听器
     */
    public void initBlueBooth(final Context context, String blueToothMac, final BuletoothPercentorListener buletoothPercentorListener) {
        if (TextUtils.isEmpty(blueToothMac)) {
            if (buletoothPercentorListener != null) {
                buletoothPercentorListener.noMacAddress();
                com.utils.LogUtils.e("没有mac地址");
            }
            return;
        }
        final ProgressBar[] proB = new ProgressBar[1];
        loadindDialog = new DialogUtils().createLoadingDialog(context, "蓝牙连接中...", new DialogUtils.OnProgressView() {
            @Override
            public void progressOnProgressView(ProgressBar progressBar) {
                proB[0] = progressBar;
            }
        });
        if (BleOperate.getInstance().initialize(context)) {

            BleOperate.getInstance().connectBleDeviceByMac(blueToothMac,
                    new SearchBleMacResultListener() {
                        @Override
                        public void hasConnected() {
                            com.utils.LogUtils.e("已经连接该设备");
                            if (loadindDialog != null && loadindDialog.isShowing()) {
                                loadindDialog.dismiss();
                            }
                            if(buletoothPercentorListener!=null){
                                buletoothPercentorListener.hasConnected();
                            }
                        }

                        @Override
                        public void searchResult(boolean value) {
                        }
                        @Override
                        public void findUUIDService() {
                        }

                        @Override
                        public void findUUIDNotify() {
                            if (loadindDialog != null && loadindDialog.isShowing()) {
                                loadindDialog.dismiss();
                            }
                            if(buletoothPercentorListener!=null){
                                buletoothPercentorListener.findUUIDNotify();

                            }
//                            sendBleCommand(context,buletoothPercentorListener);
//                            com.utils.LogUtils.e("搜索到可用的服务 ");
                        }

                        @Override
                        public void timeOut() {
                            if (loadindDialog != null && loadindDialog.isShowing()) {
                                loadindDialog.dismiss();
                            }
                            if(buletoothPercentorListener!=null){
                                buletoothPercentorListener.connectTimeout();

                            }
                        }

                        @Override
                        public void progress(long l) {
                            if (loadindDialog != null && loadindDialog.isShowing() && proB[0] != null) {
                                proB[0].setProgress((int) (l / 100));
                                com.utils.LogUtils.e("--->" + (int) (l / 100));
                            }
                        }

                        @Override
                        public void onResultReceive(byte[] result) {
                            com.utils.LogUtils.e("获取到数据");
                            if(buletoothPercentorListener!=null){
                                buletoothPercentorListener.onResultReceive(result);

                            }
                        }

                        @Override
                        public void disconect(String s) {
                            if(buletoothPercentorListener!=null){
                                buletoothPercentorListener.disconnect(s);
                            }
                        }

                        @Override
                        public void noOpen() {
                            if (loadindDialog != null && loadindDialog.isShowing()) {
                                loadindDialog.dismiss();
                            }
                        }
                    });
        } else {
            buletoothPercentorListener.noFindBuletoothDevice();

        }
    }
    public boolean  disConnectBluetoothDevice(String macAddress){
       return  BleOperate.getInstance().disconnect(macAddress);
    }
    /**
     * 向蓝牙发送通讯命令
     */
    public void sendBleCommand(String  macAddress ,byte[] command) {
        BleOperate.getInstance().writeCharacteristic(macAddress,command);
    }
    public void  disConnectBluetoothDevice(){
          BleOperate.getInstance().disconnect();
    }

}
